// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MGis.h"

#include "MainFrm.h"
#include "GISApplication.h"
#include "MapLayer.h"
#include "MapLayerExplorerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CMapLayerActionHandler_MainFrame::CMapLayerActionHandler_MainFrame(CMainFrame* pMainFrame)
{
    m_pMainFrame = pMainFrame;
}

void CMapLayerActionHandler_MainFrame::HandleEvent(CMapLayerMgr* sender, MapLayerAction* para)
{
    (m_pMainFrame->*mapActionFunc)(sender,para);
}

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()    
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
    CMapLayerActionHandler_MainFrame* mapLayerActionHandler = new CMapLayerActionHandler_MainFrame(this);
    mapLayerActionHandler->mapActionFunc = &CMainFrame::OnMaplayerAction;
    CGISApplication::getGISApplication()->getMapLayerMgr()->getEventPublisher()->Register(mapLayerActionHandler);
}

CMainFrame::~CMainFrame()
{
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

    if (!m_wndDlgBar.Create(_T("My Bar"), this, 123))
    {
        TRACE0("Failed to create mybar\n");
        return -1;
        // fail to create

    }
    m_wndDlgBar.SetSCBStyle(m_wndDlgBar.GetSCBStyle() |
        SCBS_SIZECHILD);

    m_wndDlgBar.SetBarStyle(m_wndDlgBar.GetBarStyle() |
        CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  
    m_wndDlgBar.EnableDocking(CBRS_ALIGN_ANY);

    EnableDocking(CBRS_ALIGN_ANY);

    DockControlBar(&m_wndDlgBar, AFX_IDW_DOCKBAR_LEFT);

    //if (!m_mapLayerList.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT,
    //    CRect(0,0,0,0), &m_wndMyBar, 100))
    //{
    //    TRACE0("Failed to create mybar\n");
    //    return -1;
    //}
    //m_mapLayerList.ModifyStyleEx(0, WS_EX_CLIENTEDGE);
    //m_mapLayerList.InsertColumn(0,"Name",LVCFMT_LEFT,100);
    //int itemCount = m_mapLayerList.GetItemCount();
    //int columnCount = m_mapLayerList.GetHeaderCtrl()->GetItemCount();
//     m_pMapLayerExplorerDlg = new CMapLayerExplorerDlg(&m_wndMyBar);
//     m_pMapLayerExplorerDlg->Create(IDD_DIALOG1,&m_wndMyBar);
//     m_pMapLayerExplorerDlg->ModifyStyle(0,WS_CHILD|WS_BORDER|WS_VISIBLE|WS_EX_CLIENTEDGE);
//     m_pMapLayerExplorerDlg->ShowWindow(SW_SHOW);
    
	// TODO: Delete these three lines if you don't want the toolbar to be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

void CMainFrame::OnMaplayerAction(CMapLayerMgr* sender ,MapLayerAction* para)
{
    if (para->actionType == MapLayerActionType::Add)
    {
        for (int ii = 0; ii < para->m_mapLayers.GetCount(); ii++)
        {
            addMaplayer(para->m_mapLayers.GetAt(ii));
        }
    }
    else if (para->actionType == MapLayerActionType::Delete)
    {        
    }
}

void CMainFrame::addMaplayer(CMapLayer* pMapLayer)
{
    //m_mapLayerList.InsertItem(m_mapLayerList.GetItemCount(),pMapLayer->getLayerName());
}

void CMainFrame::deleteMaplayer(CMapLayer* pMapLayer)
{
//     for (int ii = 0; ii < m_mapLayerList.GetItemCount(); ii ++)
//     {
//         TCHAR itemText[255];
//         m_mapLayerList.GetItemText(ii,0,itemText,255);
//         if (_tcscmp(itemText,pMapLayer->getLayerName()) == 0)
//         {
//             m_mapLayerList.DeleteItem(ii);
//             CGISApplication::getGISApplication()->getMapLayerMgr()->deleteMapLayer(pMapLayer);
//             return;
//         }
//     }
}

#endif //_DEBUG


// CMainFrame message handlers



